var express = require('express');
var router = express.Router();
const os = require('os');

router.get('/', function (req, res, next) {
  try {
    const netData = os.networkInterfaces();
    const netItems = [];
    for (const name in netData) {
      const ipv4Item = netData[name].find(i => i.family === 'IPv4');
      if (ipv4Item.internal === false) {
        netItems.push({
          name,
          address: ipv4Item.address,
        });
      }
    }
    res.send({
      code: 1,
      data: netItems,
      msg: '',
    });
  } catch (e) {
    res.send({
      code: 0,
      data: [],
      msg: e,
    });
  }
});

module.exports = router;
